<template>
  <div>
    <FormTable :formData="formData"
               :columnData="columnData"
               :editData="editData"
               :url="$api.game_type_getlist"
               :new_url="this.$api.game_type_insert"
               :detail_url="this.$api.game_type_info"
               :edit_url="this.$api.game_type_save"
               :is_default_edit="true"
               label-width="140px"
               :pageConfig="pageConfig"></FormTable>
  </div>
</template>

<script>
export default {
  data () {
    return {
      formData: [
        {
          name: 'game_type_name',
          label: '游戏分类标题',
          value: '',
          type: 'input'
        },
        {
          name: "p_id",
          label: "游戏上级分类",
          value: '',
          type: 'select',
          groups: [
            {
              label: '所有',
              value: ''
            },
          ]
        },
        {
          name: "display",
          label: "是否显示",
          value: '',
          type: 'select',
          groups: [
            {
              label: '所有',
              value: ''
            },
            {
              label: '隐藏',
              value: '0'
            },
            {
              label: '显示',
              value: '1'
            },
          ]
        },
      ],
      columnData: [
        {
          prop: "game_type_id",
          label: "id"
        },
        {
          prop: "game_type_name",
          label: "游戏分类标题"
        },
        {
          prop: "p_name",
          label: "游戏分类上级标题"
        },
        {
          prop: "sort",
          label: "排序",
          custom: true,
          type: 'input',
          url: this.$api.game_type_sort
        },
        {
          prop: "display",
          label: "是否显示",
          custom: true,
          type: 'switch',
          url: this.$api.game_type_status
        },
        {
          prop: 'btn_group',
          label: '操作',
          custom: true,
          type: 'btn_group',
          width: '200px',
          group: [
            {
              name: '编辑',
              type: 'edit',
              color: 'primary',
              detail_mark: 'game_type_id',
            },
            {
              name: '删除',
              type: 'delete',
              color: 'danger',
              url: this.$api.game_type_status
            }
          ]
        }
      ],
      editData: [
        {
          name: 'game_type_id',
          label: '游戏分类id',
          value: '',
          type: 'input',
          display: 'none'
        },
        {
          name: 'game_type_name',
          label: '游戏分类标题',
          value: '',
          type: 'input',
          rules:[
            { required: true, message: "请填写游戏分类标题", trigger: "blur" },
          ]
        },
        {
          name: 'p_id',
          label: '游戏上级分类',
          value: '',
          type: 'select',
          groups: [],
          rules:[
            { required: true, message: "请选择游戏上级分类", trigger: "blur" },
          ]
        },
        {
          name: 'sort',
          label: '排序',
          value: '',
          type: 'input',
        },
        {
          name: "display",
          label: "是否显示",
          value: '',
          type: 'switch',
          config: {
            'active_value': '1',
            'inactive-value': '0'
          }
        },
      ],
      pageConfig: {
        page: 1,
        pagesize: 10,
      }
    };
  },
  created () {
    this.getTopType();
  },
  methods: {
    getTopType(){
      this.$req.get(this.$api.game_type_gettoplist).then(res =>{
        let data = res.data;
        this.dataHandler(data, 1, 2, 'name', 'id');
      })
    },
    dataHandler (data, idx1, idx2, labelKey, valueKey) {
      for (var i in data) {
        if(typeof idx1 == 'number'){
          this.formData[idx1].groups.push({
            label: labelKey ? data[i][labelKey] : data[i],
            value: valueKey ? data[i][valueKey] : data[i]
          })
        }
        this.editData[idx2].groups.push({
          label: labelKey ? data[i][labelKey] : data[i],
          value: valueKey ? data[i][valueKey] : data[i],
        })
      }
      console.log(this.editData)
    },
    handleSizeChange (e) {
      console.log(e);
    },
    handleCurrentChange (e) {
      console.log(e);
    },
    submit (e) {
      console.log(e);
    },
    reset (e) {
      console.log(e);
    }
  }
}
</script>

<style lang="scss" scoped>
</style>